﻿typedef RefreshListCallback = Future<void> Function();

class RefreshableListController {
  RefreshListCallback? _onRefresh;
  RefreshListCallback? _onLoadMore;

  bool get isAttached => _onRefresh != null;

  Future<void> refresh() async {
    if (_onRefresh == null) return;
    await _onRefresh!.call();
  }

  Future<void> loadMore() async {
    if (_onLoadMore == null) return;
    await _onLoadMore!.call();
  }

  /// Internal use. The list widget calls this to wire up callbacks.
  void attachCallbacks({
    required RefreshListCallback onRefresh,
    required RefreshListCallback onLoadMore,
  }) {
    _onRefresh = onRefresh;
    _onLoadMore = onLoadMore;
  }

  /// Clears callbacks when the widget is disposed or swapped out.
  void detachCallbacks() {
    _onRefresh = null;
    _onLoadMore = null;
  }
}
